package com.leased.handler;

import com.leased.exception.DatabaseException;
import com.leased.exception.YCException;
import com.leased.pojo.AdvertInfo;
import com.leased.request.BaseQuery;
import com.leased.request.advert.AdvertInfoRequest;
import com.leased.service.AdvertInfoService;
import com.leased.utils.DateTimeUtil;
import com.leased.utils.Page;
import com.leased.utils.Validator;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by shencx on 2017/8/9.
 */
@Service
public class AdvertInfoHandler {

    Logger LOG = LoggerFactory.getLogger(AdvertInfoHandler.class);

    @Resource
    private AdvertInfoService advertInfoService;

    /**
     * 添加修改广告
     * @param advertInfoRequest
     * @throws YCException
     */
    public void addAdvertInfo(AdvertInfoRequest advertInfoRequest) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(advertInfoRequest, YCSystemStatusEnum.PARAM_EMPTY);
        Validator.isEmpty(advertInfoRequest.getStartTime(),"广告开始时间不能为空");
        Validator.isEmpty(advertInfoRequest.getEndTime(),"广告结束时间不能为空");
        Validator.isEmpty(advertInfoRequest.getAdvertContent(),"广告内容不能为空");
        AdvertInfo advertInfo = new AdvertInfo();
        advertInfo.setId(advertInfoRequest.getId());
        advertInfo.setStartTime(DateTimeUtil.convertDate(advertInfoRequest.getStartTime()));
        advertInfo.setEndTime(DateTimeUtil.convertDate(advertInfoRequest.getEndTime()));
        advertInfo.setAdvertContent(advertInfoRequest.getAdvertContent());
        try {
            if (advertInfoRequest.getId() == null) {
                advertInfoService.addAdvertInfo(advertInfo);
            }else {
                advertInfoService.updateAdvertInfo(advertInfo);
            }
        } catch (DatabaseException e) {
            LOG.error("addAdvertInfo exception",advertInfoRequest);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取广告信息列表
     * @param baseQuery
     * @return
     * @throws YCException
     */
    public Page<AdvertInfo> getAdvertInfoPage(BaseQuery baseQuery) throws YCException {
        Page<AdvertInfo> page = null;
        try {
            page = advertInfoService.fetchAdvertInfoPage(baseQuery);
            return page;
        } catch (DatabaseException e) {
            LOG.error("getAdvertInfoPage exception",baseQuery);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     * @throws YCException
     */
    public AdvertInfo getAdvertDetail(Integer id) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(id,"主键ID不能为空");
        AdvertInfo advertInfo = null;
        try {
            advertInfo = advertInfoService.fetchAdvertInfoDetail(id);
            return advertInfo;
        } catch (DatabaseException e) {
            LOG.error("getAdvertDetail exception",id);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 广告启用禁用
     * @param id
     * @param status
     * @throws YCException
     */
    public void modifyStatus(Integer id,Integer status) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(id,"主键ID不能为空");
        Validator.isEmpty(status,"状态值不能为空");
        AdvertInfo advertInfo = new AdvertInfo();
        advertInfo.setId(id);
        advertInfo.setBeUsed(status);
        try {
            advertInfoService.updateAdvertInfo(advertInfo);
        } catch (DatabaseException e) {
            LOG.error("modifyStatus exception",id,status);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }
}
